/**
 * 
 */
package cre.jaxws.publish;

import org.apache.cxf.Bus;
import org.apache.cxf.BusFactory;
import org.apache.cxf.bus.managers.DestinationFactoryManagerImpl;
import org.apache.cxf.frontend.ClientProxyFactoryBean;
import org.apache.cxf.frontend.ServerFactoryBean;
import org.apache.cxf.transport.ConduitInitiatorManager;
import org.apache.cxf.transport.DestinationFactoryManager;
import org.apache.cxf.transport.local.LocalTransportFactory;

import cre.order.HelloWorld;
import cre.order.HelloWorldImpl;

/**
 * @author Cre.Gu
 *
 */
public class ServerPublish {

	public static void main(String[] args) throws InterruptedException {
		Bus bus = BusFactory.getDefaultBus();
		LocalTransportFactory localTransport = new LocalTransportFactory();
		

		DestinationFactoryManager dfm = bus.getExtension(DestinationFactoryManager.class);

		dfm.registerDestinationFactory("http://schemas.xmlsoap.org/soap/http", localTransport);
		dfm.registerDestinationFactory("http://schemas.xmlsoap.org/wsdl/soap/http", localTransport);
		dfm.registerDestinationFactory("http://cxf.apache.org/bindings/xformat", localTransport);
		dfm.registerDestinationFactory("http://cxf.apache.org/transports/local", localTransport);

		ConduitInitiatorManager extension = bus.getExtension(ConduitInitiatorManager.class);
		extension.registerConduitInitiator("http://cxf.apache.org/transports/local", localTransport);
		extension.registerConduitInitiator("http://schemas.xmlsoap.org/wsdl/soap/http", localTransport);
		extension.registerConduitInitiator("http://schemas.xmlsoap.org/soap/http", localTransport);
		extension.registerConduitInitiator("http://cxf.apache.org/bindings/xformat", localTransport);

		ServerFactoryBean sf = new ServerFactoryBean();
		sf.setAddress("local://hello");
		sf.setServiceBean(new HelloWorldImpl());
		sf.setServiceClass(HelloWorld.class); // Optionally specify the service
												// interface
		sf.create();

		ClientProxyFactoryBean cf = new ClientProxyFactoryBean();
		cf.setAddress("local://hello");
		cf.setServiceClass(HelloWorld.class); // Optionally specify the service interface
		
		HelloWorld h = (HelloWorld) cf.create();
		System.out.println(h);
		System.out.println(h.sayHi("gu"));
		
		Thread.sleep(5 * 60 * 1000);
		System.out.println("Server exiting");
		System.exit(0);
	}
}
